package day05.text04.service;

import day05.text04.dao.Dao;
import day05.text04.pojo.Goods;

import java.io.IOException;
import java.net.DatagramPacket;
import java.util.ArrayList;

public class Service {

    private Dao dao = new Dao();


    public ArrayList<Goods> getGoodsList() throws IOException {
        return dao.read();
    }
    public void addGoods(ArrayList<Goods> list) throws IOException {

        dao.addGoods(list);
    }

    public void editGoods(int id,Goods goods) throws IOException {
        dao.editGoods(id,goods);
    }
    public void deleteGoods(int id) throws IOException {
        dao.deleteGoods(id);
    }


    public boolean isExists(String id,String name) throws IOException {
        boolean exists = false;
        ArrayList<Goods> read = dao.getGoodsList();
        for (Goods goods : read) {
            if(goods.getId().equals(id)){
                System.out.println("你输入的编号错误,请重新输入");
                break;
            }else if(goods.getName().equals(name)){
                System.out.println("你输入的名字重复,请重新输入");
                break;
            }else {
                exists=true;
                break;
            }
        }
        return exists;
    }
    public boolean isExists(String id) throws IOException {
        boolean exists = false;
        ArrayList<Goods> read = dao.getGoodsList();
        for (Goods goods : read) {
            if(goods.getId().equals(id)){
               exists=true;
               break;
            }else {
                System.out.println("你输入的编号错误,请重新输入");
                break;
            }
        }
        return exists;
    }
    public boolean isExists1(String id) throws IOException {
        boolean exists = false;
        ArrayList<Goods> read = dao.getGoodsList();
        for (Goods goods : read) {
            if(goods.getId().equals(id)){
                System.out.println("你输入的编号错误,请重新输入");

                break;
            }else {
                exists=true;
                break;
            }
        }
        return exists;
    }

    public int getIndexById(String id) throws IOException {
        //遍历集合
        ArrayList<Goods> read = dao.getGoodsList();
        for (int i = 0; i < read.size(); i++) {
            Goods goods = read.get(i);
            if (goods.getId().equals(id)) {
                return i;
            }
        }

        return -1;
    }


}
